import { Api } from './../../../providers/api-service';
import { InvitationFriendHomePage } from './../invitation-friend-home/invitation-friend-home';
import { NoticeService } from './../../../providers/notice-service';
import { UrlService } from './../../../providers/url-service';
import { ButtonService } from '../../../providers/button.service';
import { Component } from '@angular/core';
import { NavController, NavParams, LoadingController } from 'ionic-angular';
import { ImgService } from '../../../providers/camera-service';

/**
 * 发布挑战
 * @export
 * @class NewChallengePage
 */
@Component({
  selector: 'page-new-challenge',
  templateUrl: 'new-challenge.html',
})

export class NewChallengePage {
  hideor = false; //隐藏邀请好友
  gaming: string = "nes";
  userlist = [];//选择的好友
  steps = {
    step1: true,
    step2: false,
    step3: false
  };

  wizardShow1List = [];
  MyfriendList = [];//好友列表

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public urlServer: UrlService,
    public noticeService: NoticeService,
    public imgService: ImgService,
    public api: Api,
    public loadingCtrl: LoadingController,
    public btnSer: ButtonService
  ) {
    this.GetDekaronClassify();
    console.log(this.pageData);
  }
  newLeave = new Date().toISOString().substring(0, new Date().toISOString().indexOf("T"));



  ionViewDidLoad() {
    console.log('发布挑战 NewChallengePage');
  }
  /**
   * 
   */
  ionViewDidEnter() {
    let friendList = eval('(' + this.api.getLocalStorage("myfriendList") + ')');
    if (friendList != null && friendList.length >= 0) {
      this.MyfriendList = friendList;
      localStorage.removeItem("myfriendList");
      this.steps = { step1: false, step2: true, step3: false };
      console.table(this.MyfriendList);
    }
  }
  /**
   * 打开好友列表
   */
  openInvitationFriend_home() {
    this.navCtrl.push(InvitationFriendHomePage);
  }

  GetDekaronClassify() {
    this.urlServer.GetDekaronClassify()
      .then(data => {
        this.wizardShow1List = data.Data;
      })
  }
  endtimea = false;

  /**
   * 选择挑战类型
   */
  wizarItem(item) {
    this.steps.step1 = false;
    this.steps.step2 = true;
    console.table(item);
    if (item.Classify == 30) {
      this.endtimea = true;
    }
    this.pageData.Classify = item.Classify;
    console.log(this.pageData);
  }
  pageData = {
    Title: "",//标题
    Describe: "",
    PicUrl: "",
    Integral: 100,
    Week: 1, //几周
    WeekRate: 0,//每周最少打卡天数
    StartDate: "",//开始时间
    EndDate: "",//结束时间
    Classify: 10, //类型
    Pattern: 0, //模式
    IsReward: false,
    RewardMessage: "",
    PunishmentMessage: "",
    State: "",
    City: "上海",
    Number: 0,
    applylist: [],
    GFIntegral: "",
    RewardPunishmentVideo: ""
  };

  /**
   * 下一步
   */
  nextBtn1() {
    if (this.steps.step3) {
      console.table(this.pageData);
      this.InsertNewDekaronInfo();
    } else if (this.steps.step2) {
      if (this.pageData.Title == "") {
        this.api.showNoticeByToast(1, "标题不能为空");
        return false;
      } else if (this.pageData.Pattern == 0) {
        this.api.showNoticeByToast(1, "请选择挑战模式");
        return false;
      } else if (this.pageData.Week == 0) {
        this.api.showNoticeByToast(1, "请填写(1~26)周数");
        return false;
      } else if (this.pageData.WeekRate == 0) {
        this.api.showNoticeByToast(1, "请选择每周上传天数");
        return false;
      } else if (this.pageData.Classify == 30) {
        if (this.pageData.Number == 0) {
          this.api.showNoticeByToast(1, "请填写数值");
          return false;
        }

      } else if (this.pageData.Integral < 100) {
        this.api.showNoticeByToast(1, "请输入参与战豆(100-10000)");
        return false;
      } else if (this.pageData.StartDate == "") {
        this.api.showNoticeByToast(1, "请填选择开始时间");
        return false;
      } else if (this.pageData.EndDate == "") {
        this.api.showNoticeByToast(1, "请选择结束时间");
        return false;
      } else if (this.pageData.Describe == "") {
        this.api.showNoticeByToast(1, "请填写挑战内容");
        return false;
      } else if (this.pageData.Pattern == 30) {
        if (this.pageData.applylist.length == 0) {
          this.api.showNoticeByToast(1, "请邀请战友");
          return false;
        }
      }
      console.table(this.pageData);
      this.steps.step2 = false;
      this.steps.step3 = true;

    }
  }


  // showMoreActionFlag=true;
  /**
   * 发布挑战
   */
  InsertNewDekaronInfo() {
    this.urlServer.InsertNewDekaronInfo(this.pageData)
      .then(data => {

        if (data.Code == 0) {
          this.noticeService.presentAlert("提示", "发布成功");
          // this.showMoreActionFlag=true;
          let obj = data.Data;
          let type;
          let num;
          let Url = obj.Url;
          let RedBag = obj.RedBag;
          let Integral = obj.Integral;
          if (Url) {
            num = 0;
            type = 3;
          } else if (RedBag) {
            num = RedBag;
            type = 1;
          } else {
            num = Integral;
            type = 2;
          }
          let tittle = this.pageData.Title;
          this.toast1(type, Url, num, tittle);
        } else {
          this.noticeService.presentConfirm("提示", data.Msg, () => {
            //跳转充值页面
            // this.navCtrl.push();
          });
        }
      })
  }
  /**
   * 红包积分
   * @param type 
   * @param url 
   * @param num 
   * @param title 
   */
  toast1(type, url, num, title) {
    var thistype;
    if (type == 1) {//红包
      thistype = 2;
      this.hongbao(num);
    } else if (type == 2) {//积分
      thistype = 1;
      this.jifen(num);
    } else {//分享
      thistype = 3;
      // toastFenxiang();
    }
    if (thistype != 3) {
      this.api.postBody("V1/GetNewTargetReward", {
        "Type": thistype,
        "Number": num,
        "desc": "挑战:" + title
      }).then(data => {
        console.log("领取奖励");
        if (data.Code == 0) {
        }
      });
    }

  }


  jifen(num) {
    if (!num || num == "null") {
      num = 0;
    }
    this.noticeService.presentConfirm("恭喜您", "获得" + num + "元红包，已存入余额", () => {
      //跳转充值页面
      this.navCtrl.pop();
    });
  }

  hongbao(num) {
    // var str = "";
    // str += '<div id="toast348748564" class="ub ub-ac ub-pc" style="position: fixed;z-index:6666; top: 0em;left: 0em;width: 100%;height: 100%;background-color:rgba(0,0,0,0.25);">';
    // str += '    <div class="ub ub-ac ub-ver" style="width: 70%;height: 50%;">';
    // str += '        <div class="ub-img ub ub-ver ub-ac ub-pc ub-f1" style="background-image: url(../css/img/redbao.png);width: 100%;">';
    // str += '            <div class="ub ub-ver ub-ac ub-pc" style="margin-top: 6em;font-size: 1.2em;color: #fed901;">';
    // str += '                <div >恭喜您</div>';
    // str += '                <div class="umar-t">获得红包</div>';
    // str += '            </div>';
    // str += '        </div>';
    // str += '        <div id="yes65648" (click)="" class="ub ub-ac ub-pc" style="width: 8em;height: 2em;font-size: 1.2em;background-color:#fed901;border-radius: 3px;color: #F75F47;">确定</div>';
    // str += '    </div>';
    // str += '</div>';
    // $("body").append(str);

    // appcan.button("#yes65648", "ani-act", function () {
    //   $("#toast348748564").remove();
    //   jinqian(num);
    // })
    this.noticeService.presentConfirm("恭喜您", "获得红包", () => {
      //跳转充值页面
      // this.navCtrl.push();

    });


  }
  /**
   * 模式
   */
  moshi() {
    let options = [{
      type: 'radio',
      label: '单人',
      value: 10
    },
    {
      type: 'radio',
      label: '对战',
      value: 20
    },
    {
      type: 'radio',
      label: '多人',
      value: 30
    }];
    this.noticeService.roleAlert("选择类型", options, data => {
      console.log(data);
      if (data >= 20) {
        this.hideor = true;
      } else {
        this.hideor = false;
      }
      this.pageData.Pattern = data;
    });
  }
  /**
   * 
   */
  WeekRate() {
    let options = [];
    for (let index = 1; index < 8; index++) {
      options.push({
        type: 'radio',
        label: index,
        value: index
      })
    }
    this.noticeService.roleAlert("选择上传天数", options, data => {
      this.pageData.WeekRate = data;
    });
  }

  /**
   * 挑战周期
   * @param event 
   */
  Weekdate(event, value) {
    if (value < 1) {
      this.pageData.Week = 1;
    } else if (value > 26) {
      this.pageData.Week = 26;
    }
    this.endTime();
  }
  /**
   * 选择开始时间后 调用
   * @param event 
   */
  changeDate(event) {
    this.endTime();
  }

  /**
   * 结束时间
   */
  endTime() {
    if (this.pageData.StartDate != "" && this.pageData.Week != 0) {
      this.pageData.EndDate = this.btnSer.getDateFromCurrentDate(this.pageData.StartDate, this.pageData.Week * 7);
    }
  }
  /**
   * 挑战说明
   * @param content 
   */
  filteremoji(event, value) {
    let content = value;
    let ranges = ['\ud83c[\udf00-\udfff]', '\ud83d[\udc00-\ude4f]', '\ud83d[\ude80-\udeff]'];
    let emojireg = content.replace(new RegExp(ranges.join('|'), 'g'), '');
    this.pageData.Describe = emojireg;
  }

  Media = {
    isUploadImg: true,
    isUploadVoide: true,
    loadImgsrc: "",
    loadVodeSrc: ""
  }
  loading;
  /**
   * 上传图片
   */
  uploadimg() {
    this.imgService.showPicActionSheet("Dynamic", data => {
      let msg = eval("(" + data["response"] + ")");
      alert(msg.Data);
      this.Media.loadImgsrc = msg.Data;
      this.pageData.PicUrl = msg.Data;
      this.Media.isUploadImg = false;
    })
  }
  /**
   *上传视频 
   */
  uploadvoide() {
    this.imgService.startMedia(data => {
      alert(JSON.stringify(data));
      this.Media.loadVodeSrc = "";
      this.Media.isUploadVoide = false;
    })
  }

  btn3(value) {
    if (true) {
      value = value.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, '')
    }
  }

  /**
   *
   */
  GetNewTargetReward() {

  }
  /**
   * 添加视频
   */
  addVideo() {
    this.imgService.startMedia((data) => {
      alert(JSON.stringify(data));
      this.pageData.RewardPunishmentVideo = "";
    });

  }
}
